package com.atguigu.tingshu.payment.service.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.common.rabbit.service.RabbitService;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wechat.pay.java.service.payments.model.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
@SuppressWarnings({"all"})
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private RabbitService rabbitService;
    @Override
    public PaymentInfo savePaymentInfo(Long userId, String paymentType, String orderNo, BigDecimal payAmount) {
        //可以在保存之前，先查询是否有交易记录，如果有，则跳过
        PaymentInfo paymentInfo = this.getOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, orderNo).eq(PaymentInfo::getUserId, userId));
        //判断Wrapper条件的结果是否为空
        if (null!=paymentInfo) {
            return paymentInfo;
        }

        //空值null，需要创建对象，null值.属性 空指针
        PaymentInfo paymentInfo1 = new PaymentInfo();

        //赋值
        paymentInfo1.setUserId(userId);
        paymentInfo1.setPaymentType(paymentType);
        paymentInfo1.setOrderNo(orderNo);
        paymentInfo1.setAmount(payAmount);
        paymentInfo1.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
        paymentInfo1.setPayWay(SystemConstant.ORDER_PAY_WAY_WEIXIN);
        paymentInfo1.setContent(paymentType.equals(SystemConstant.PAYMENT_TYPE_ORDER) ? "订单支付" : "余额支付");

        //保存数据
        this.save(paymentInfo1);
        return paymentInfo1;
    }

    /**
     * 更新交易记录状态 为已支付
     * @param transaction
     */
    @Override
    public void updatePaymentStatus(Transaction transaction) {
        //支付完成 更新用户交易记录 payment_info
        //根据订单号 查询是否已经有支付记录 有支付记录则不需要 记录 直接返回
        PaymentInfo paymentInfo = this.getOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, transaction.getOutTradeNo()));
        //查询出的订单支付信息 为空 证明未保存信息 用户未支付 则 不需要更改订单状态
        if (null==paymentInfo ) {
            return;
        }
        //为空，则证明为保存过，需要更新用户交易记录状态未支付 为 已支付 payment_info
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_PAID); //订单状态 已支付
        paymentInfo.setOrderNo(transaction.getOutTradeNo()); //商户内部订单号 transaction.getOutTradeNo()==paymentInfo.getOutTradeNo()
        paymentInfo.setOutTradeNo(transaction.getTransactionId());//微信支付内部 订单号 transaction.getOutTradeNo()==paymentInfo.getOutTradeNo()
        paymentInfo.setCallbackTime(new Date());//回调时间
        paymentInfo.setCallbackContent(transaction.toString());//回调信息
        this.updateById(paymentInfo);
        //支付完成 更新订单表 支付状态
        //判断区分是订单，还是充值业务，根据不同的业务，选择不同的路由键
        String routingKey=paymentInfo.getPaymentType().equals(SystemConstant.PAYMENT_TYPE_ORDER)?MqConst.ROUTING_ORDER_PAY_SUCCESS:MqConst.ROUTING_RECHARGE_PAY_SUCCESS;
        //发送RabbitMq消息 实现异步更新订单表 支付状态  transaction.getOutTradeNo()==paymentInfo.getOutTradeNo()
        rabbitService.sendMessage(MqConst.EXCHANGE_ORDER,routingKey,paymentInfo.getOutTradeNo());
    }

    /**
     * 关闭交易记录
     * @param orderNo
     * @return
     */
    @Override
    public boolean closePayment(String orderNo) {
        //  创建对象
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
        //  设置更新条件
        boolean result = this.update(paymentInfo, new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, orderNo));
        //  返回
        return result;
    }
}
